package com.open.party.cloud.view.home.dangWuMiShu

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.*
import androidx.core.content.ContextCompat
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.TouPiaoGuanBean
import com.open.party.cloud.model.TouPiaoGuanLiSaveResultVO
import com.open.party.cloud.model.TouPiaoGuanLiSaveVO
import com.open.party.cloud.model.TouPiaoItemBean
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.switchTabView.style1.UIUtil
import kotlinx.android.synthetic.main.dwms_tou_piao_guan_li_detail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.indices
import kotlin.collections.iterator
import kotlin.collections.set

class DwmsTouPiaoGuanLiDetailActivity : AppEventBusTitleBaseActivity(), CompoundButton.OnCheckedChangeListener {

    private var viewModel: BizViewModel? = null
    private var id = ""

    override fun getLayoutResId(): Int = R.layout.dwms_tou_piao_guan_li_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("投票")

        id = intent.getStringExtra("id") as String
        viewModel = BizViewModel()
        viewModel!!.getTouPiaoGuanDetail(id)

        submitBtn.setOnClickListener {
            saveTouPiaoGuanLiResult()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<TouPiaoGuanBean>) {
        if (result.eventType != "getTouPiaoGuanDetail") {
            return
        }
        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    showData(result.data)
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().show("Token过期，请重新登录")
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showData(data: TouPiaoGuanBean) {
        titleTv.text = XUtils.string().getNotNullValue(data.title)
        contentTv.text = XUtils.string().getNotNullValue(data.content)

        if (data.votingCandidateList == null || data.votingCandidateList.size == 0) {
            return
        }

        this.votingCandidateList = data.votingCandidateList

        var qTitleTv: TextView? = null

        when (data.type) {
            0 -> {
                val view01 = LayoutInflater.from(this).inflate(R.layout.kao_shi_item_01, null)
                qTitleTv = view01.findViewById(R.id.qTitleTv)

                val radioGroup: RadioGroup = view01.findViewById(R.id.radioGroup)
                for (subIndex in data.votingCandidateList.indices) {
                    val subItem = data.votingCandidateList[subIndex]

                    val cb = RadioButton(this)
                    cb.text = XUtils.string().getNotNullValue(subItem.name)
                    cb.setTextColor(ContextCompat.getColor(this@DwmsTouPiaoGuanLiDetailActivity, R.color.page_content))
                    cb.paint.textSize = UIUtil.sp2px(this, 13f).toFloat()
                    cb.setOnCheckedChangeListener(this)
                    radioGroup.addView(cb, subIndex)
                }

                itemsView.addView(view01)
            }
            1 -> {
                val view02 = LayoutInflater.from(this).inflate(R.layout.kao_shi_item_02, null)
                qTitleTv = view02.findViewById(R.id.qTitleTv)

                val checkBoxView: LinearLayout = view02.findViewById(R.id.checkBoxView)

                for (subIndex in data.votingCandidateList.indices) {
                    val subItem = data.votingCandidateList[subIndex]

                    val cb = CheckBox(this)
                    cb.text = XUtils.string().getNotNullValue(subItem.name)
                    cb.setTextColor(ContextCompat.getColor(this@DwmsTouPiaoGuanLiDetailActivity, R.color.page_content))
                    cb.paint.textSize = UIUtil.sp2px(this, 13f).toFloat()
                    cb.setOnCheckedChangeListener(this)
                    checkBoxView.addView(cb, subIndex)
                }

                itemsView.addView(view02)
            }
        }

        if (qTitleTv != null) {
            // 标题
            qTitleTv.text = XUtils.string().getNotNullValue(data.title)
        }
    }

    ///////////////////////////////////////////////////////////////////////
    private var votingCandidateList: ArrayList<TouPiaoItemBean>? = null
    private val resultMap = HashMap<String, Boolean>()

    override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
        val key = buttonView.text.toString()
        resultMap[key] = isChecked
    }

    private fun saveTouPiaoGuanLiResult() {
        if (resultMap.size == 0) {
            XUtils.toast().show("至少选择一项选项")
            return
        }

        val saveVO = TouPiaoGuanLiSaveVO()
        saveVO.questionnaireId = id
        saveVO.resultList = ArrayList()
        //
        for ((key, value) in resultMap) {
            if (value) {
                val saveResultVO = TouPiaoGuanLiSaveResultVO()
                saveResultVO.questionnaireId = id
                saveResultVO.itemId = id
                saveResultVO.answer = key

                for (itemBean in votingCandidateList!!) {
                    if (itemBean.name == key) {
                        saveResultVO.optId = itemBean.id
                    }
                }

                saveVO.resultList.add(saveResultVO)
            }
        }

        viewModel!!.saveTouPiaoGuanLiResult(saveVO)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBus2Callback(result: ResultInfo<Boolean>) {
        if (result.eventType != "saveTouPiaoGuanLiResult") {
            return
        }
        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data) {
                    XUtils.toast().show("投票成功")
                    finish()
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().show("Token过期，请重新登录")
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}